package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuRegionConfig;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dunhuangshiku.DunHuangShiKuModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/20 22:08
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_ACTION, desc = "敦煌石窟行为操作  0攻击 1接受 2绕过 3标记 4下一个区域")
public class DunHuangShiKuActionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuActionReqMsg req = DunHuangShiKuProto.DunHuangShiKuActionReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        ret = DunHuangShiKuMgr.canEnterActivitying(player,activityId);
        if(ret != 0){
            return;
        }
        if(req.getStyle() != 1 && req.getStyle() != 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        if(req.getType() == 4 && req.getStyle() == 1){
            // 判断道具是否足够
            DunHuangShiKuRegionConfig regionConfig = DunHuangShiKuMgr.regionConfigMap.get(req.getRegionId());
            if(regionConfig == null){
                ret = GameErrorCode.E_DUN_HUANG_SHI_KU_CONFIG_ERROR;
                return;
            }
            DunHuangShiKuRegionConfig nextRegionConfig = DunHuangShiKuMgr.regionConfigMap.get(req.getRegionId() + 1);
            if(nextRegionConfig == null){
                ret = GameErrorCode.E_DUN_HUANG_SHI_KU_NOT_NEXT_REGION;
                return;
            }
            Property property = PropertyHelper.parseStringToProperty(regionConfig.getConsumeCondition());
            if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
                ret = GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
                return;
            }
            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(property);
            DunHuangShiKuModule dunHuangShiKuModule = player.getModule(DunHuangShiKuModule.class);
            dunHuangShiKuModule.nextRegionProperty = property;
        }
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_DUN_HUANG_SHI_KU_ACTION, req.toBuilder(), req.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DunHuangShiKuProto.DunHuangShiKuGetTeamListRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetTeamListRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_ACTION, builder);
        }
    }
}
